package com.fafa.pos.util.newland;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;

import java.io.*;


public class CommonUtils {
	private static String TAG = "CommonUtils";
	/**
	 * 判断字符串是否为空串
	 * @param str 待检查的字符串
	 */
	public static boolean isEmptyString(String str) {
		if (null == str || "".equals(str)) {
			return true;
		}
		return false;
	}
	/**
	 * 安装应用
	 * 
	 * @param apkPath
	 *            APK的存放路径
	 * @return
	 */
	public static boolean installApk(Context context, String apkPath) {
		try {
			if (!isEmptyString(apkPath) && apkPath.endsWith(".apk")) {
				File file = new File(apkPath);
				if (file != null && file.exists() && file.canRead()) {
					String result = executeCmd("pm install -r " + "\""
							+ apkPath + "\"");
					Log.i(TAG, "执行pm install -r命令，APK路径：" + apkPath + "，结果："
							+ result);
					String pkgName = AppUtils.getAndroidArchiveInfo(context, apkPath).packageName;
					if(AppUtils.isInstalled(context, pkgName)){
						return true;
					} else {
						return false;
					}
						
				} else {
					Log.e(TAG, "APK文件不可读，APK路径：" + apkPath);
				}
			} else {
				Log.i(TAG, "待安装的文件不是APK文件，文件路径：" + apkPath);
			}
			return false;
		} catch (IOException e) {
			return false;
		}
	}
	/**
	 * 执行命令
	 * 
	 * @param cmd
	 *            命令字符串
	 * @return
	 * @throws IOException
	 */
	public static String executeCmd(String cmd) throws IOException {
		ProcessBuilder processBuilder = new ProcessBuilder("/system/bin/sh");
		File dir = new File("system/bin");
		processBuilder.directory(dir);
		processBuilder.redirectErrorStream(true);
		Process process = processBuilder.start();
		BufferedReader br = new BufferedReader(new InputStreamReader(
				process.getInputStream()));
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
				process.getOutputStream()));
		PrintWriter pw = new PrintWriter(bw, true);
		pw.println(cmd);
		pw.println("exit");

		StringBuffer sb = new StringBuffer();
		String tempStr;
		while ((tempStr = br.readLine()) != null) {
			sb.append(tempStr);
			sb.append("\n");
		}
		br.close();
		bw.flush();
		bw.close();
		pw.flush();
		pw.close();
		process.destroy();
		return sb.toString();
	}	
	/**
	 * 安装应用
	 * 
	 * @param apkPath
	 *            APK的存放路径
	 * @return
	 */
	public static boolean installDS(Context context,String apkPath) {
		try {
			if (!CommonUtils.isEmptyString(apkPath) && apkPath.endsWith(".apk")) {
//				Intent intent=new Intent(Intent.ACTION_VIEW_HIDE);//TODO lyc 需要了解最新的写法
				Intent intent=new Intent();
				intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				intent.setDataAndType(Uri.fromFile(new File(apkPath)),"application/vnd.android.package-archive");
				Log.i(TAG, "安装应用，文件路径：" + apkPath);
				context.startActivity(intent);
				startDS(context);
			} else {
				Log.i(TAG, "待安装的文件不是APK文件，文件路径：" + apkPath);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}
	/**
	 * 传入动作调入mtms
	 * */
	private static void startDS(Context context) {
		ComponentName componentName = new ComponentName(  
	            "com.landicorp.android.deviceservice",  
	            "com.landicorp.android.deviceservice.NewLandDeviceService");  
		Intent intent = new Intent();
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		intent.setComponent(componentName);
		context.startActivity(intent);
	}	
}
